﻿using SqlSugar;
using System.Linq.Expressions;

namespace oldislandapi
{
    public class DBContext
    {
        private static IConfiguration configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json").Build();

        private static readonly string _connectionString = configuration["ConnectionStrings:mysqlUser"];

        private DBContext() { }

        public static SqlSugarClient DB
        {
            get
            {
                var db = new SqlSugarClient(
                    new ConnectionConfig
                    {
                        ConnectionString = _connectionString,
                        DbType = DbType.MySql,
                        IsAutoCloseConnection = true,//自动释放数据库连接，如果存在事务，在事务结束后释放
                        InitKeyType = InitKeyType.Attribute//从实体特性中读取主键自增列信息
                    });

                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine($"{sql}\r\n{db.Utilities.SerializeObject(pars.ToDictionary(it => it.Value))}");
                };

                return db;
            }
        }
    }
}
